#!/usr/bin/env python
"""

   reverse_complement_dna.py

        does what it says on the box


   Copyright (C) 2007 Leo Goodstadt

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   as published by the Free Software Foundation; version 2
   of the License

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
"""

import sys
import string



#
# prepare table
#
complement_table = string.maketrans('ACGTNnacgt-_RYKMSWBVDHrykmswbvdh', 'TGCANntgca-_YRMKWSVBHDyrmkwsvbhd')
allchars = string.maketrans('','')
allbutacgt = allchars.translate(allchars, 'ACGTNnacgt-_')

def reverse_complement (seq):
    #print seq
    seq = "".join(list(reversed(list(seq))))
    return seq.translate(complement_table, allbutacgt)

def complement (seq):
    #print seq
    #seq = "".join(list(reversed(list(seq))))
    return seq.translate(complement_table, allbutacgt)


def unit_test():
    cdna = "CTGCAATTGTACCACTGGGTGGAAAGGATCATTCTGCACAGAGACAGTTTCTACCTGTGATCCTGAACATGACCC"+\
            "TCCACACCACTGTAGCAGAGGAGCAACCTGCATTTCATTACCTCATGGATACACCTGTTTCTGTCCTCTAGGA"+\
            "ACCACTGGAATCTACTGTGAACAAG"

    reversed = "CTTGTTCACAGTAGATTCCAGTGGTTCCTAGAGGACAGAAACAGGTGTATCCATGAGGTA"\
               "ATGAAATGCAGGTTGCTCCTCTGCTACAGTGGTGTGGAGGGTCATGTTCAGGATCACAGG"\
               "TAGAAACTGTCTCTGTGCAGAATGATCCTTTCCACCCAGTGGTACAATTGCAG"
    assert(reverse_complement(cdna) == reversed)


if __name__ == '__main__':
    from optparse import OptionParser

    parser = OptionParser(version="%prog 1.0")
    parser.add_option("-d", "--dna", dest="dnafile",
                      metavar="FILE",
                      type="string",
                      help="name and path of dna file in fasta format")

    parser.set_defaults(dnafile = "")

    # get help string
    import StringIO
    f =StringIO.StringIO()
    parser.print_help(f)
    helpstr = f.getvalue()
    (options, args) = parser.parse_args()

    # mandatory options
    if options.dnafile == "":
        unit_test()
        print "\nUnit test passed. See below for how to run this module:\n"
        print helpstr
        sys.exit(0)


    if options.dnafile == '-':
        dnafile = sys.stdin
    else:
        dnafile = open(options.dnafile)

    seq = ''
    import line_break
    for line in dnafile.xreadlines():
        if not len(line):
            continue
        if line[0] == '>':
            if len(seq):
                print line_break.line_wrap(reverse_complement(seq))
                seq = ''
            print line[:-1]
        else:
            seq = seq + line
    else:
        if len(seq):
            print line_break.line_wrap(reverse_complement(seq))
            seq = ''

